package org.squareland;

import org.squareland.entities.DwarfEntity;
import org.squareland.entities.Entity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class SceneView extends View {
	
	public SceneView(Context context) {
		super(context);
		gameWorld = new GameWorld();
		setBackgroundColor(Color.GRAY);
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		for (int i = 0; i < gameWorld.world.size(); ++i) {
			drawEntity(gameWorld.world.get(i), canvas);
		}
	}
	
	// TODO put this method into Entity
	protected void drawEntity(Entity entity, Canvas canvas) {
		Paint paint = new Paint();
		for (int i = 0; i < entity.getHeight(); ++i) {
			for (int j = 0; j < entity.getWidth(); ++j) {
				paint.setColor(entity.at(i, j));
				//paint.setColor(Color.BLACK);
				canvas.drawRect((entity.x + j) * PIXEL_SIZE,
						        (entity.y + i) * PIXEL_SIZE,
						        (entity.x + j + 1) * PIXEL_SIZE,
						        (entity.y + i + 1) * PIXEL_SIZE,
						        paint);
			}
		}
		
		if (entity instanceof DwarfEntity) {
			paint.setColor(Color.CYAN);
			canvas.drawRect(entity.x * PIXEL_SIZE,
			        	   (entity.y - 1) * PIXEL_SIZE,
			        	   (entity.x + entity.width) * PIXEL_SIZE,
			        	   (entity.y - 0.5f) * PIXEL_SIZE,
			        	   paint);

			paint.setColor(Color.RED);
			canvas.drawRect(entity.x * PIXEL_SIZE,
			        	   (entity.y - 1) * PIXEL_SIZE,
			        	   (entity.x + entity.width * ((DwarfEntity)entity).HP / (float)((DwarfEntity)entity).maxHP) * PIXEL_SIZE,
			        	   (entity.y - 0.5f) * PIXEL_SIZE,
			        	   paint);

		}
	}
	
	private final int PIXEL_SIZE = 10;
	public GameWorld gameWorld;

}
